package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.dubbo.api.CommentApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.vo.CommentVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.server.interceptor.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class CommentsService {
    @DubboReference
    private CommentApi commentApi;
    @DubboReference
    private UserInfoApi userInfoApi;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    //发布评论
    public void comment(String movementId, String comment) {
        Long userId = UserHolder.getUserId();
        Comment comment1 = new Comment();
        comment1.setPublishId(new ObjectId(movementId));
        comment1.setContent(comment);
        comment1.setUserId(userId);

        comment1.setCommentType(CommentType.COMMENT.getType());
        commentApi.save(comment1);
        String key = "COMMENT_" + comment1.getPublishId().toHexString();
        String commentCount = (String) redisTemplate.opsForHash().get(key, "COMMENT_COUNT");
        if (commentCount != null) {

            Integer i = Integer.valueOf(commentCount) + 1;

            redisTemplate.opsForHash().put(key, "COMMENT_COUNT", i.toString());
        } else {
            redisTemplate.opsForHash().put(key, "COMMENT_COUNT", "1");
        }
    }


    //分页查询评理列表
    public PageResult findComments(String movementId, Integer page, Integer pagesize) {
        Long userId = UserHolder.getUserId();
        //1、调用API查询评论列表
        List<Comment> list = commentApi.findComments(movementId,CommentType.COMMENT,page,pagesize);
        //2、判断list集合是否存在
        if(CollUtil.isEmpty(list)) {
            return new PageResult();
        }
        //3、提取所有的用户id,调用UserInfoAPI查询用户详情
        List<Long> userIds = CollUtil.getFieldValues(list, "userId", Long.class);
        Map<Long, UserInfo> map = userInfoApi.findByIds(userIds, null);
        //4、构造vo对象
        List<CommentVo> vos = new ArrayList<>();
        for (Comment comment : list) {
            UserInfo userInfo = map.get(comment.getUserId());
            if(userInfo != null) {
                CommentVo vo = CommentVo.init(userInfo, comment);
                Object o = redisTemplate.opsForHash().get("COMMEND_" + vo.getId(), "USER_LIKE_" + userId);
                if (o==null){
                    vo.setHasLiked(0);
                }else {
                    vo.setHasLiked(1);
                }

                vos.add(vo);
            }
        }
        //5、构造返回值
        return new PageResult(page,pagesize,0l,vos);
    }

    //点赞评论
    public Integer commentLike(String id) {
        Long userId = UserHolder.getUserId();
        //从redis存取
        String o = (String) redisTemplate.opsForHash().get("COMMEND_" + id, "LIKE_COUNT");
        redisTemplate.opsForHash().put("COMMEND_" + id, "USER_LIKE_" + userId, "1");
        if (o == null) {
            redisTemplate.opsForHash().put("COMMEND_" + id, "LIKE_COUNT", "1");
            return 1;
        } else {
            redisTemplate.opsForHash().increment("COMMEND_" + id, "LIKE_COUNT", 1);
            return Integer.valueOf(o) + 1;
        }
    }

    //取消喜欢评论
    public Integer commentDislike(String id) {
        Long userId = UserHolder.getUserId();
        String o = (String) redisTemplate.opsForHash().get("COMMEND_" + id, "LIKE_COUNT");
        redisTemplate.opsForHash().delete("COMMEND_" + id, "USER_LIKE_" + userId, "1");
        redisTemplate.opsForHash().increment("COMMEND_" + id, "LIKE_COUNT", -1);
        return Integer.valueOf(o) - 1;
    }


}
